/**
 * @author Helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.ClientsDao) SALE.dao.local.ClientsDao = function(){
	var clientsDoc;
}

SALE.dao.local.ClientsDao.prototype = {	
	
		initClientsDoc: function() {
			if(!this.clientsDoc) {
				this.clientsDoc = LocalXMLFileUtil.getClientsDoc();
			}
			return this.clientsDoc;
		},
		
		saveClientDoc : function() {
			LocalXMLFileUtil.saveXML(this.clientsDoc,LocalXMLFileUtil.getClientsFile());
		},
		
		insertClientInfo : function(clientJson) {
			var root = this.clientsDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			clientJson["id"] = id;
			root.setAttribute("maxid",id);
			var clientNode = this.clientsDoc.createElement("client");
			
			this.updateClient(clientJson, clientNode);			
			root.appendChild(clientNode);
			
			this.saveClientDoc();
		},
		
		modifyClientInfo : function(clientJson) {
			var clientNode = this.searchClientByID(clientJson.id);
			
			this.updateClient(clientJson, clientNode);
			
			this.saveClientDoc();
		},
		
		deleteClientInfo : function(id) {
			var clientNode = this.searchClientByID(id);
			var root = this.clientsDoc.documentElement;
			root.removeChild(clientNode);
		},
				
		updateClient : function(clientJosn, clientNode) {
			
			for(var attri in clientJosn) {	
				
				if(attri.indexOf("old") != 0 && clientJosn[attri] && clientJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, clientNode);
					if(attriNodes != null && attriNodes.length > 0) {
						clientNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.clientsDoc.createElement(attri);
					var contextNode = this.clientsDoc.createCDATASection(clientJosn[attri]);
					attriNode.appendChild(contextNode);
					clientNode.appendChild(attriNode);
				}
			}
		},
		
		searchClientByID : function(id) {
			var root = this.clientsDoc.documentElement;
			var objArray = Ext.query("/client:has(id:nodeValue(" + id+ "))", root);
			var result;
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchClientJsonByID : function(id) {
			
			var resultNode = this.searchClientByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildClientsJson: function() {
			var root = this.initClientsDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/client", root);
				var clientsJson = {clients: []};
				
				Ext.each(objArray, function(client) {
					var children = client.childNodes;
					var clientJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!clientJson[child.nodeName] && child.childNodes[0] != null) {
								clientJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					clientsJson.clients.push(clientJson);
				});
				
				return clientsJson;
			}
		}
	
}
